package rs.etf.es.algorithm.states;

import rs.etf.es.algorithm.Algorithm;
import rs.etf.es.algorithm.AlgorithmState;
import rs.etf.es.algorithm.JTextAreaLogger;
import rs.etf.es.algorithm.Node;
import rs.etf.es.algorithm.NodeAddressBook;

public class SendPiMessageState implements AlgorithmState {

    private Algorithm algorithm;
    private Node sender, receiver;
    public String senderName, receiverName;

    @Override
    public void performAction() {
	JTextAreaLogger.instance().log("");

	if (sender != null && receiver != null && algorithm != null) {
	    receiver.sendPiMessageToThisNode(sender, algorithm);
	}

    }

    @Override
    public void setAlgorithm(Algorithm algorithmClass) {
	algorithm = algorithmClass;
    }

    public Node getSender() {
	return sender;
    }

    public void setSender(Node sender) {
	this.sender = sender;
	senderName = sender.getName();
    }

    public Node getReceiver() {
	return receiver;
    }

    public void setReceiver(Node receiver) {
	this.receiver = receiver;
	receiverName = receiver.getName();
    }

    public AlgorithmState clone() {
	SendPiMessageState newState = new SendPiMessageState();
	newState.setAlgorithm(algorithm);
	newState.senderName = new String(sender.getName());
	newState.receiverName = new String(receiver.getName());
	return newState;
    }

    @Override
    public void refreshNodes() {
	sender = NodeAddressBook.instance().getNode(senderName);
	receiver = NodeAddressBook.instance().getNode(receiverName);
    }

}
